/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.mainApp.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import ca.usherbrooke.mainApp.MainAppAgent;
import ca.usherbrooke.mainApp.view.newSimulation.NewSimulationPanel;
import ca.usherbrooke.mainApp.view.openSimulation.PanelOpenSimulation;
import ca.usherbrooke.utils.ExceptionTool;

/**
 * @author Cody Stoutenburg
 * 
 */
public class MainPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private final JPanel centerPanel;
	public static boolean newSimulationIsCreating = false;

	private final MainAppAgent agent;

	public MainPanel(MainAppAgent agent) {
		this.setLayout(new BorderLayout());

		this.agent = agent;
		centerPanel = new JPanel();

		JMenuBar menu = new JMenuBar();

		JMenu simulation = new JMenu("Simulation");
		JMenu tools = new JMenu("Tools");
		menu.add(simulation);
		menu.add(tools);

		// create new simulation
		JMenuItem itemNewSimulation = new JMenuItem("New");
		itemNewSimulation.setEnabled(false);
		itemNewSimulation.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				createSimulation();
			}
		});

		JMenuItem itemOpenSimulation = new JMenuItem("Open");
		itemOpenSimulation.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				openSimulation();
			}
		});

		JMenuItem itemSaveSimulation = new JMenuItem("Save");
		itemSaveSimulation.setEnabled(false);
		JMenuItem itemSaveAsSimulation = new JMenuItem("Save As");
		itemSaveAsSimulation.setEnabled(false);

		JMenuItem itemExitSimulation = new JMenuItem("Exit");
		itemExitSimulation.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				exit();
			}
		});

		simulation.add(itemNewSimulation);
		simulation.add(itemOpenSimulation);
		simulation.addSeparator();
		simulation.add(itemSaveSimulation);
		simulation.add(itemSaveAsSimulation);
		simulation.addSeparator();
		simulation.add(itemExitSimulation);

		JMenuItem itemStartRma = new JMenuItem("Start RMA");
		itemStartRma.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				startRma();
			}
		});
		tools.add(itemStartRma);

		this.add(menu, BorderLayout.NORTH);
		this.add(centerPanel, BorderLayout.CENTER);
	}

	public JPanel getCenterPanel() {
		return centerPanel;
	}

	private void exit() {
		agent.stopJade();
	}

	private void openSimulation() {
		JFileChooser chooser = new JFileChooser(
				MainAppAgent.DIRECTORY_SIMULATION);
		chooser.setDialogTitle("open a simulation");
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		chooser.setAcceptAllFileFilterUsed(false);

		int retVal = chooser.showOpenDialog(this);
		if (retVal == JFileChooser.APPROVE_OPTION) {
			File directory = chooser.getSelectedFile();
			if (directory.isDirectory()) {
				centerPanel.removeAll();
				centerPanel.validate();
				try {
					centerPanel.add(new PanelOpenSimulation(this.agent,
							directory));
					centerPanel.revalidate();
					centerPanel.repaint();
				} catch (IOException ex) {
					ExceptionTool.showError("", ex);
				}
			} else {
				ExceptionTool.showError("a simulation is a directory");
			}
		}
	}

	private void createSimulation() {
		if (!newSimulationIsCreating) {
			centerPanel.removeAll();
			centerPanel.validate();
			centerPanel.add(new NewSimulationPanel());
			centerPanel.revalidate();
			centerPanel.repaint();
			newSimulationIsCreating = true;
		}
	}

	private void startRma() {
		this.agent.startAgent("rma", "jade.tools.rma.rma", null);
	}
}
